package com.xh.easyretry.listener;

import com.xh.easyretry.annotations.XHRetry;
import com.xh.easyretry.common.XHRetryContext;
import lombok.extern.slf4j.Slf4j;

import java.util.Objects;

@Slf4j
public class LastRetryListener<T extends XHRetryContext> extends RetryListener<T> {

    @Override
    public void listen(T t) {
        Integer currentRetryCount = t.getCurrentRetryCount();
        int maxRetryCount = t.getXhRetry().retryCount();
        if (!Objects.equals(maxRetryCount, currentRetryCount)) {
            return;
        }
        XHRetry xhRetry = t.getXhRetry();
        if (Objects.isNull(xhRetry)) {
            throw new IllegalArgumentException("xhRetry is null");
        }
        String lastRetryListenerMethodName = xhRetry.lastRetryListener();
        doWithSpecifyMethod(t, lastRetryListenerMethodName);
    }
}
